package com.jxthxr.monitor.utils;

import com.alibaba.fastjson.JSONObject;
import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.notify.AbstractStatusChangeNotifier;
import java.util.Map;
import reactor.core.publisher.Mono;

/**
 * @Author silence.Z
 * @Date 2022/6/10 11:00
 * @PackageName:com.jxthxr.monitor.utils
 * @ClassName: DingDingNotifier
 * @Description: TODO
 * @Version 1.0
 */
public class DingDingNotifier extends AbstractStatusChangeNotifier {
	public DingDingNotifier(InstanceRepository repository) {
		super(repository);
	}

	@Override
	protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
		String serviceName = instance.getRegistration().getName();
		String serviceUrl = instance.getRegistration().getServiceUrl();
		String status = instance.getStatusInfo().getStatus();
		Map<String, Object> details = instance.getStatusInfo().getDetails();
		StringBuilder str = new StringBuilder();
		str.append("监控报警 : 【" + serviceName + "】");
		str.append("【状态：】" + status);
		str.append("【服务地址：】" + serviceUrl);
		str.append(" 【详情：】" + JSONObject.toJSONString(details));
		return Mono.fromRunnable(() -> {
			DingDingMessageUtil.sendTextMessage(str.toString());
		});
	}
}
